/** @format */

const express = require('express');
const bodyParser = require('body-parser');
const webpack = require('webpack');
const webpackDevMiddleware = require('webpack-dev-middleware');
const webpackHotMiddleware = require('webpack-hot-middleware');
const WebpackConfig = require('./webpack.config');
const cors = require('cors');
const router = express.Router();

const app = express();
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));

app.use(router);
const compiler = webpack(WebpackConfig);

app.use(
  webpackDevMiddleware(compiler, {
    publicPath: '/__build__/',
    stats: {
      colors: true,
      chunks: false,
    },
  }),
);

app.use(webpackHotMiddleware(compiler));

app.use(express.static(__dirname));

// get
simpleGetRouterRegister();

// post
simplePostRouterRegister();

// error
simpleErrorRouterRegister();

// extend
extendRouterRegister();

// interceptor
interceptorRouterRegister();

// config
configRouterRegister();

const port = process.env.PORT || 8706;
module.exports = app.listen(port, () => {
  console.log(`Server listening on http://localhost:${port}, Ctrl+C to stop`);
});

function simpleGetRouterRegister() {
  router.get('/simple/get', function (req, res) {
    res.json({
      msg: `hello world`,
    });
  });

  router.get('/base/get', function (req, res) {
    res.json(req.query);
  });
}

function simplePostRouterRegister() {
  router.post('/base/post', function (req, res) {
    res.json(req.body);
  });

  router.post('/base/buffer', function (req, res) {
    const msg = [];
    req.on('data', chunk => {
      if (chunk) {
        msg.push(chunk);
      }
    });
    req.on('end', () => {
      const buf = Buffer.concat(msg);
      res.json(buf.toJSON());
    });
  });
}

function simpleErrorRouterRegister() {
  router.get('/error/get', function (req, res) {
    if (Math.random() > 0.5) {
      res.json({
        msg: 'hello world',
      });
    } else {
      res.status(500);
      res.end();
    }
  });

  router.get('/error/timeout', function (req, res) {
    setTimeout(() => {
      res.json({
        msg: `hello world`,
      });
    }, 3000);
  });
}

function extendRouterRegister() {
  router.get('/extend/get', function (req, res) {
    res.json({
      msg: 'hello world',
    });
  });
  router.post('/extend/post', function (req, res) {
    res.json(req.body);
  });
  router.options('/extend/options', function (req, res) {
    res.end();
  });
  router.delete('/extend/delete', function (req, res) {
    res.end();
  });
  router.head('/extend/head', function (req, res) {
    res.end();
  });
  router.patch('/extend/patch', function (req, res) {
    res.json(req.body);
  });
  router.put('/extend/put', function (req, res) {
    res.json(req.body);
  });
  // Response泛型
  router.get('/extend/user', function (req, res) {
    res.json({
      code: 0,
      message: 'OK',
      result: {
        name: 'Jack',
        age: 18,
      },
    });
  });
}

function interceptorRouterRegister() {
  router.get('/interceptor/get', function (req, res) {
    res.end('hello');
  });
}

function configRouterRegister() {
  router.post('/config/post', function (req, res) {
    res.json(req.body);
  });
}
